package com.ops.memcache.core;

import com.ops.uitl.common.Common;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * lenovo
 * 2017/5/10
 * untitled
 */
public class EHCacheManager implements ICacheManager {
	private static EHCacheManager instance;
	private static CacheManager commonManager;
	private static CacheManager customManager;

	private EHCacheManager() throws Exception {
//		commonManager = createCommonManager("ehcache_wadelib.xml");
		customManager = createCustomManager("ehcache.xml");
	}

	public static EHCacheManager getInstance() throws Exception {
		if(instance == null) {
			instance = new EHCacheManager();
		}

		return instance;
	}

	public String[] getCacheNames() throws Exception {
//		String[] commonNames = commonManager.getCacheNames();
		String[] customNames = customManager.getCacheNames();
		ArrayList cacheNames = new ArrayList();

		int i;
//		for(i = 0; i < commonNames.length; ++i) {
//			cacheNames.add(commonNames[i]);
//		}

		for(i = 0; i < customNames.length; ++i) {
			if(!cacheNames.contains(customNames[i])) {
				cacheNames.add(customNames[i]);
			}
		}

		return (String[])((String[])cacheNames.toArray(new String[0]));
	}

	public ICache[] getCaches() throws Exception {
		ArrayList caches = new ArrayList();
		String[] names = this.getCacheNames();

		for(int i = 0; i < names.length; ++i) {
			ICache cache = this.getCache(names[i]);
			caches.add(cache);
		}

		return (ICache[])((ICache[])caches.toArray(new ICache[0]));
	}

	public void addCache(String cache_name) throws Exception {
		customManager.addCache(cache_name);
	}

	public ICache getCache(String cache_name) throws Exception {
		Cache cache = customManager.getCache(cache_name);
//		if(cache == null) {
//			cache = commonManager.getCache(cache_name);
//		}

		return cache == null?null:new EHCache(cache);
	}

	public void removeCache(String cache_name) throws Exception {
		customManager.removeCache(cache_name);
	}

	public ICacheElement getCacheElement(String cache_name, Serializable key) throws Exception {
		ICache cache = this.getCache(cache_name);
		if(cache != null) {
			ICacheElement element = cache.get(key);
			if(element != null) {
				return element;
			}
		}

		return null;
	}

	public void putCacheElement(String cache_name, Serializable key, Serializable value) throws Exception {
		ICache cache = this.getCache(cache_name);
		if(cache != null) {
			cache.put(key, value);
		}

	}

	public void removeCacheElement(String cache_name, Serializable key) throws Exception {
		ICache cache = this.getCache(cache_name);
		if(cache != null) {
			cache.remove(key);
		}

	}

	public int getStatus(String cache_name) throws Exception {
		return customManager.getStatus().intValue();
	}

	public void shutdown() throws Exception {
		customManager.shutdown();
	}

	private static CacheManager createCommonManager(String file) throws Exception {
		synchronized(EHCacheManager.class) {
			return commonManager == null?new CacheManager(Common.getInstance().getClassResourceStream(file)):commonManager;
		}
	}

	private static CacheManager createCustomManager(String file) throws Exception {
		synchronized(EHCacheManager.class) {
			return customManager == null?new CacheManager(Common.getInstance().getClassResourceStream(file)):customManager;
		}
	}
}
